In [1]:
import plotly.offline as pyo

from plotly.graph_objs import *

import chart_studio.plotly as py

import pandas as pd
from pandas import DataFrame
In [2]:
pyo.offline.init_notebook_mode()
In [3]:
healthcare = pd.read_csv(r"../Data/lifeExpectancy-HealthcareSpending.csv", index_col = 0)
healthcare.head()
Out[3]:
Country Life expectancy Spending per capita($)
0 United States 78.94 9024
1 Switzerland 82.85 6786
2 Norway 81.75 6081
3 Netherlands 81.30 5276
4 Germany 80.84 5119
In [4]:
pyo.iplot([{'type' : 'scatter',
          'mode' : 'markers',
          'x' : healthcare['Spending per capita($)'],
          'y' : healthcare['Life expectancy'],
          'text' : healthcare['Country'],}])
In [5]:
healthcare['hovertext'] = healthcare.apply(lambda x: 
        "<b>{}</b><br>Life expectancy: {:.0f} years<br>Healthcare spend per capita: ${:,}".format(x['Country'],
                                                                                                x['Life expectancy'],
                                                                                                x['Spending per capita($)']),
                                          axis = 1)

healthcare.head()
Out[5]:
Country Life expectancy Spending per capita($) hovertext
0 United States 78.94 9024 <b>United States</b><br>Life expectancy: 79 ye...
1 Switzerland 82.85 6786 <b>Switzerland</b><br>Life expectancy: 83 year...
2 Norway 81.75 6081 <b>Norway</b><br>Life expectancy: 82 years<br>...
3 Netherlands 81.30 5276 <b>Netherlands</b><br>Life expectancy: 81 year...
4 Germany 80.84 5119 <b>Germany</b><br>Life expectancy: 81 years<br...
In [6]:
healthcare.loc[0,'hovertext']
Out[6]:
'<b>United States</b><br>Life expectancy: 79 years<br>Healthcare spend per capita: $9,024'
In [7]:
def addTextNote(row):
    if row['Life expectancy'] > 70 and row['Spending per capita($)'] < 6000:
        return ''
    else:
        return "  " + row['Country']
In [8]:
healthcare['textnote'] = healthcare.apply(addTextNote, axis = 1)
healthcare.head()
Out[8]:
Country Life expectancy Spending per capita($) hovertext textnote
0 United States 78.94 9024 <b>United States</b><br>Life expectancy: 79 ye... United States
1 Switzerland 82.85 6786 <b>Switzerland</b><br>Life expectancy: 83 year... Switzerland
2 Norway 81.75 6081 <b>Norway</b><br>Life expectancy: 82 years<br>... Norway
3 Netherlands 81.30 5276 <b>Netherlands</b><br>Life expectancy: 81 year...
4 Germany 80.84 5119 <b>Germany</b><br>Life expectancy: 81 years<br...
In [10]:
healthcareTrace = {'type' : 'scatter',
                  'mode' : 'markers',
                  'text' : healthcare['hovertext'],
                  'x' : healthcare['Spending per capita($)'],
                  'y' : healthcare['Life expectancy'],
                  'hoverinfo' : 'text',
                  'showlegend' : False,
                  'marker' : {'color' : '#B22222'}}
data = Data([healthcareTrace])

layout = {'title' : 'Healthcare Spending and Life Expectancy',
          'hovermode' : 'closest',
         'xaxis' : {'title' : 'Healthcare spending per capita',
                   'tickformat' : '$,',
                   'range' : [0, healthcare['Spending per capita($)'].max()*1.05]},
         'yaxis' : {'title' : 'Life expectancy (years)',
                   'range' : [healthcare['Life expectancy'].min()*0.95,
                             healthcare['Life expectancy'].max()*1.05]}}
fig = Figure(data=data, layout=layout)
pyo.iplot(fig)
In [38]:
textNote = [{'type' : 'scatter',
                  'mode' : 'text',
                  'textposition' : 'middle right',
                  'text' : healthcare['textnote'],
                  'x' : healthcare['Spending per capita($)'],
                  'y' : healthcare['Life expectancy'],
                  'hoverinfo' : 'none',
                   'showlegend' : False}]
dataUpdated = Data([healthcareTrace]) + textNote
fig = Figure(data=dataUpdated, layout=layout)
pyo.iplot(fig)
In [36]:
fig['layout']['xaxis'].update({'range' : [0, healthcare['Spending per capita($)'].max() * 1.15]})
pyo.iplot(fig)
In [ ]: